package com.search;

import java.util.HashMap;
import java.util.Map;

public class CheckInclusion {

	public static void main(String[] args) {
		String a  ="q";
		String b  ="q";
		boolean res = new CheckInclusion().checkInclusion(a, b);
		System.out.println(res);
	}
	 public boolean checkInclusion(String s1, String s2) {
		 	Map<Character,Integer> need = new HashMap();
		 	Map<Character,Integer> Windows = new HashMap();
		 	for(char ch : s1.toCharArray())
		 		need.put(ch ,need.getOrDefault( ch , 0)+ 1);
	        int left = 0,right = 0;
	        int vaild = 0;
        	while(right < s2.length()) {
        		char ch1 = s2.charAt(right);
        		right++;
        		if(need.containsKey(ch1)) {
        			Windows.put(ch1 ,Windows.getOrDefault( ch1 , 0)+ 1);
        			if(Windows.get(ch1).equals(need.get(ch1)))
        				vaild ++;
        		}
        		while(right - left >= s1.length()) {
        			if(need.size() == vaild) {
        				return true;
        			}
        			char ch2 = s2.charAt(left);        			
        			left++;
        			if(need.containsKey(ch2)) {
            			if(Windows.get(ch2).equals(need.get(ch2)))
            				vaild --;
            			Windows.put(ch2 ,Windows.getOrDefault(ch2 ,0)- 1);
            		}
        		}
        	}
	        return false;
	 }
}
